package com.sharemarking.application.common.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sharemarking.application.common.BaseSpecification;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.Room;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.RoomRepo;

@Controller
@RequestMapping("room")
public class RoomController {
	
	@Autowired
	private RoomRepo dao;
	
	@Autowired
	private EmployeeRepo er;
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	@PreAuthorize("hasPermission('sys:room',1)")
	public void index(Room e,Integer page,Integer size,Model model){
		if(page==null)page=0;
		else page-=1;
		if(size==null)size=30;
		Sort order=new Sort(Direction.DESC,"id");
		PageRequest req=new PageRequest(page,size,order);
		Page<Room> p=dao.findAll(new BaseSpecification<Room>(e), req);
		model.addAttribute("page", p);
		model.addAttribute("search", e);
	}
	
	/**
	 * 获取实体接口
	 * @param e
	 * @return
	 */
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	@PreAuthorize("hasPermission('sys:room',1)")
	public String get(Room obj,Model model){
		if(obj.getId()!=0) obj=dao.findOne(obj.getId());
		model.addAttribute("obj",obj);
		return "room/form";
	}
	
	@RequestMapping(value="{id}",method=RequestMethod.POST)
	@Transactional
	@PreAuthorize("hasPermission('sys:room',#target.id==0?8:4)")
	public String post(Room target,Model model,HttpServletRequest request){
		//添加
		if(target.getId()==0){
			
			//关联关系
			Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
			Integer employeeId1=Integer.parseInt(request.getParameter("employeeId1"));
			
			Employee e=er.findOne(employeeId);
			Employee e1=er.findOne(employeeId1);
			
			target.setEmployee(e);
			target.setEmployee1(e1);
			
			dao.save(target);
			return "redirect:index";
		}else {
			//编辑
			Room e=dao.findOne(target.getId());
			if(e==null)return null;
			
			//关联关系
			Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
			Integer employeeId1=Integer.parseInt(request.getParameter("employeeId1"));
			
			Employee e1=er.findOne(employeeId);
			Employee e2=er.findOne(employeeId1);
			
			e.setEmployee(e1);
			e.setEmployee1(e2);
			e.setApplyTime(target.getApplyTime());
			e.setName(target.getRoomNo());
			e.setName(target.getName());
			e.setStartTime(target.getStartTime());
			e.setEndTime(target.getEndTime());
			e.setState(target.getState());
			
			return "redirect:index";
		}
	}
	/**
	 * 逻辑删除
	 * @param id
	 */
//	@PreAuthorize("hasPermission('sys:room ',16)")
	@RequestMapping(value="{id}/delete",method=RequestMethod.GET)
	@Transactional
	public String delete(@PathVariable("id")Integer id){
		dao.findOne(id).setDelFlag(true);
		return "redirect:../index";
	}
	
}
